package com.bdtop.dbl.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import android.util.Log;

public class HttpClient {

	private static final String TAG = HttpClient.class.getSimpleName();

	public static final String GET = "GET";
	public static final String POST = "POST";
	public static final String UTF_8 = "UTF-8";
	public static final String httpStatusCode = "httpStatusCode";
	public static final int ConnectTimeout = 20 * 1000;
	public static final int ReadTimeout = 20 * 1000;

	public static String post(String reqURL, Map<String, String> params) {
		HttpURLConnection conn = null;
		BufferedOutputStream out = null;
		BufferedInputStream in = null;
		try {
			URL url = new URL(reqURL);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod(POST);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setConnectTimeout(ConnectTimeout);
			conn.setReadTimeout(ReadTimeout);

			byte[] data = convertParams(params).getBytes(UTF_8);
			if (data.length > 0) {
				out = new BufferedOutputStream(conn.getOutputStream(), 8 * 1024);
				out.write(data);
				out.flush(); // clear cache, send data
			}

			int statusCode = conn.getResponseCode();
			if (statusCode == 200) {
				in = new BufferedInputStream(conn.getInputStream(), 8 * 1024);
				return inputStreamToString(in);
			}
			else {
				return httpStatusCode + statusCode;
			}
		} catch (Exception e) {
			Log.i(TAG, e.getMessage());
			throw new RuntimeException("HttpClient post Exception");
		} finally {
			if (conn != null) conn.disconnect();
			try {
				if (in != null) in.close();
				if (out != null) out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	public static String get(String reqURL, Map<String, String> params) {
		HttpURLConnection conn = null;
		BufferedInputStream in = null;
		try {
			String data = convertParams(params);
			if (data.length() > 0) reqURL = reqURL + "?" + data;
			URL url = new URL(reqURL);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod(GET);
			conn.setRequestProperty("Content-Type", "text/html; charset=" + UTF_8);
			conn.setConnectTimeout(ConnectTimeout);
			conn.setReadTimeout(ReadTimeout);

			conn.connect();

			int statusCode = conn.getResponseCode();
			if (statusCode == 200) {
				in = new BufferedInputStream(conn.getInputStream(), 8 * 1024);
				return inputStreamToString(in);
			}
			else {
				return httpStatusCode + statusCode;
			}
		} catch (Exception e) {
			Log.i(TAG, e.getMessage());
			throw new RuntimeException("HttpClient get Exception");
		} finally {
			if (conn != null) conn.disconnect();
			try {
				if (in != null) in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static String convertParams(Map<String, String> params) {
		if (params == null || params.isEmpty()) return "";
		StringBuffer sb = new StringBuffer();
		for (String key : params.keySet()) {
			if (sb.length() > 1) sb.append("&");
			sb.append(key).append("=").append(params.get(key));
		}
		return sb.toString();
	}

	private static String inputStreamToString(InputStream in) {
		StringBuffer out = new StringBuffer();
		byte[] b = new byte[8 * 1024];
		try {
			for (int n; (n = in.read(b)) != -1;) {
				out.append(new String(b, 0, n));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toString();
	}

}